package cn.csust.net2.manager.server.security;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Service;

import cn.csust.net2.manager.shared.exception.BeyondAuthorityException;
import cn.csust.net2.manager.shared.service.SecurityService;
import cn.csust.net2.manager.shared.util.ServiceConstant;

/**
 * @author 韩忠金
 * @version 创建时间：2012-1-25 下午09:42:59 
 * 类说明 
 * 业务切面：1.权限切面2.日志切面
 */
@Service
@Aspect
public class AuthorityAspect {
	static Logger logger = Logger.getLogger ( AuthorityAspect.class.getName () ) ;
	public static final String EXECUTION_STR = " execution(* cn.csust.net2.manager.server.service.impl.*.*(..)) && !target(cn.csust.net2.manager.server.service.impl.LoginServiceImpl) && !target(cn.csust.net2.manager.server.service.impl.SecurityServiceImpl) ";	
	@Resource(name = ServiceConstant.SERVICE_NAME_SECURITY)
	private SecurityService securityService ;
	public SecurityService getSecurityService() {
		return securityService;
	}
	public void setSecurityService(SecurityService securityService) {
		this.securityService = securityService;
	}
	@Before(value = EXECUTION_STR)
	public void beforeSayHello(JoinPoint joinPoint) throws BeyondAuthorityException {
//		System.out.println(this.getLastStr(joinPoint.getTarget().getClass().getName()));
//		System.out.println(securityService.testSecurity(AuthCurrentUser.get(),this.getLastStr(joinPoint.getTarget().getClass().getName())));
		if(AuthCurrentUser.get()==null)
			System.out.println("当前无用户");
		System.out.println(AuthCurrentUser.get().getUserID()+"访问"+this.getLastStr(joinPoint.getTarget().getClass().getName()));
		
		boolean authority = securityService.testSecurity(AuthCurrentUser.get(),this.getLastStr(joinPoint.getTarget().getClass().getName()));
		if(authority){
			//打印;
			logger.info("权限验证成功");
		}else{
			logger.info("发生越权");
			throw new BeyondAuthorityException("当前用户"+"未达到访问"+this.getLastStr(joinPoint.getTarget().getClass().getName())+"权限");
		}
	}
	/**
	 * 等到字符串最后一个.号后面的字符串
	 */
	public String getLastStr(String org){
		if(org.length()<=0||org.indexOf(".")<0)
			return "";
		int i = org.lastIndexOf(".");
		int j = org.lastIndexOf("ServiceImpl");
		return org.substring(i+1,j);
	}
	
	@After(value = EXECUTION_STR)
	public void afterSayHello(JoinPoint joinPoint) {
		
		logger.info("系统执行完成"+joinPoint.getTarget().getClass().getName()+"方法调用："+joinPoint.getSignature().getName()+"参数为：");
		Object[] args=joinPoint.getArgs();
	      for(int i=0;i<args.length;i++){
	    	  logger.info("第"+i+"个："+args[i]);
	   }
		
	}
//
//	@Around(value = EXECUTION_STR)
//	public void aroundSayHello(ProceedingJoinPoint joinPoint) throws Throwable {
//		System.out.println("Around Before !! ");
//		joinPoint.proceed();
//		System.out.println("Around After !! ");
//	}

	@AfterThrowing(value =EXECUTION_STR, throwing = "ex")
	public void afterThrowingSayHello(Exception ex) {
		System.out.println("抛出异常了 : " + ex.getMessage());
	}
//
//	@AfterReturning(value = EXECUTION_STR, returning = "reval")
//	public void afterReturningSayHello(String reval) {
//		System.out.println("After Returning : " + reval);
//	}
}
